package com.ssm_basic.egnotice1.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ssm_basic.egnotice1.base.response.CodeMsg;
import com.ssm_basic.egnotice1.base.response.JsonResult;
import com.ssm_basic.egnotice1.mapper.NoticeMapper;
import com.ssm_basic.egnotice1.pojo.dto.NoticeAddParam;
import com.ssm_basic.egnotice1.pojo.dto.NoticeUpdateParam;
import com.ssm_basic.egnotice1.pojo.entity.Notice;
import com.ssm_basic.egnotice1.pojo.vo.NoticeDetailInfoVO;
import com.ssm_basic.egnotice1.pojo.vo.NoticeListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@Api(tags = "资讯测试模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @ApiOperation("新增公告")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody NoticeAddParam param){
        log.debug("新增公告,param:" + param);
        Notice notice = new Notice();
        BeanUtils.copyProperties(param,notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(1L);
        int num = noticeMapper.insert(notice);

        return num > 0 ? JsonResult.success() :new JsonResult(CodeMsg.OPERATION_ERROR);
    }

    @ApiOperation("公告列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult list(String title,Integer type){

        log.debug("title="+title+",type="+type);
        List<NoticeListVO> list = noticeMapper.list(title,type);
        return JsonResult.success(list);
    }

    @ApiOperation("公告详情")
    @ApiOperationSupport(order = 30)
    @GetMapping("detail")
    public JsonResult detail(Long id){
        log.debug("id="+id);
        NoticeDetailInfoVO detailInfo = noticeMapper.detail(id);
        return JsonResult.success(detailInfo);
    }

    @ApiOperation("删除公告")
    @ApiOperationSupport(order = 40)
    @ApiImplicitParam(name = "id",value = "公告id",required = true)
    @PostMapping("del")
    public JsonResult del(Long id){
        log.debug("id="+id);
        int num = noticeMapper.del(id);
        return num > 0? JsonResult.success() : new JsonResult(CodeMsg.OPERATION_ERROR);
    }

    @ApiOperation("修改公告")
    @ApiOperationSupport(order = 50)
    @PostMapping("update")
    public JsonResult update(@RequestBody @ApiIgnore NoticeUpdateParam param){
        log.debug("param = " + param);
        Notice notice = new Notice();
        BeanUtils.copyProperties(param,notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.update(notice);
        return num > 0? JsonResult.success() : new JsonResult(CodeMsg.OPERATION_ERROR);
    }
}
